package com.shitu.soulemo.a;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shitu.base.net.model.Tabs;
import com.shitu.soulemo.R;
import com.shitu.soulemo.rn.RNPayActivity;

import java.util.ArrayList;
import java.util.List;

public class TjGridAdpater extends BaseAdapter {
    List<Tabs> data = new ArrayList<>();
    Fragment fragment;

    public TjGridAdpater(Fragment fr) {
        fragment = fr;
    }

    @Override
    public int getCount() {
        if (data.size() > 6) {
            return 6;
        } else {
            return data.size();
        }
    }

    public void setData(List<Tabs> data) {
        this.data = data;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tj_grid_item, null);
        final Tabs tab = data.get(position);
        TextView name = view.findViewById(R.id.name);
        name.setText(tab.getName());
        TextView price = view.findViewById(R.id.price);
        price.setText(tab.getPrice() + "元");
        TextView count = view.findViewById(R.id.count);
        count.setText(tab.getCount() + "人");
        TextView time = view.findViewById(R.id.time);
        time.setText(tab.getTimeCount() + "天");
        TextView address = view.findViewById(R.id.address);
        address.setText(tab.getHopeWorkPlace());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragment.getActivity() != null) {
                    RNPayActivity.start(fragment.getActivity(), tab.getId(), tab);
                }
            }
        });
        ImageView image = view.findViewById(R.id.image);
        if (Math.random() > 0.5) {
            image.setImageResource(R.drawable.fuwu_icon);
        } else {
            image.setImageResource(R.drawable.wupin_icon);
        }
        return view;
    }
}
